<template>
  <a-spin :spinning="loading">
    <export-excel :col="col" :data="data">
      <template #default="{ exportExcel }">
        <a-dropdown v-if="group" :disabled="loading">
          <a-button>
            <template #icon><download-outlined /></template>
            导出Excel
          </a-button>
          <template #overlay>
            <a-menu @click="handleExportExcel(exportExcel, $event)">
              <slot>
                <a-menu-item key="1">
                  <file-text-outlined />
                  导出本页
                </a-menu-item>
                <a-menu-item key="2">
                  <file-text-outlined />
                  导出全部
                </a-menu-item>
              </slot>
            </a-menu>
          </template>
        </a-dropdown>
        <a-button v-else @click="handleExportExcel(exportExcel, { key: '1' })">
          <template #icon><download-outlined /></template>
          导出Excel
        </a-button>
      </template>
    </export-excel>
  </a-spin>
</template>

<script lang="ts" setup>
import ExportExcel from '@/components/export-excel/index.vue'
import { DownloadOutlined, FileTextOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'

interface Props {
  col: any
  data: any[]
  group?: boolean
  loading?: boolean
}

interface Emits {
  (e: 'on-export', func: Function): void
  (e: 'on-export-all', func: Function): void
  (e: 'on-custom-export', key: string, func: Function): void
}

const props = withDefaults(defineProps<Props>(), {
  col: () => ({}),
  data: () => [],
  group: false,
  loading: false
})
const emit = defineEmits<Emits>()

async function handleExportExcel(func: Function, e: any) {
  if (!props.data.length) {
    message.error('表格数据为空')
    return
  }

  if (e.key === '1') {
    emit('on-export', func)
  } else if (e.key === '2') {
    emit('on-export-all', func)
  } else {
    emit('on-custom-export', e.key, func)
  }
}
</script>

<script lang="ts">
export default {
  name: 'export-excel-group'
}
</script>
